﻿// fct_cfg_options.cpp: 实现文件
//

#include "pch.h"
#include "MFCApplication1.h"
#include "afxdialogex.h"
#include "fct_cfg_options.h"

#include <nlohmann/json.hpp>
#include <fstream>
#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
using json = nlohmann::json;

// fct_cfg_options 对话框

IMPLEMENT_DYNAMIC(fct_cfg_options, CDialogEx)

fct_cfg_options* mcfg_options = NULL;
fct_cfg_options::fct_cfg_options(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MFC_TEST_CFG_OPTIONS, pParent)
{
	mcfg_options = this;
}

fct_cfg_options::~fct_cfg_options()
{
	mcfg_options = NULL;
}

void fct_cfg_options::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CFG_WRETE_IMEI, m_write_imei_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_IMEI, m_read_imei_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_CARD, m_read_card_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_LTE_NB_CAL, m_lte_nb_cal_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_WCDMA_CAL, m_wcdma_cal_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_GSM_CAL, m_gsm_cal_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_CSQ, m_read_csq_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_NET, m_read_network_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_MODE, m_read_mode_checkBox); // 确保ID和变量名正确
	DDX_Control(pDX, IDC_CFG_READ_VERSION, m_read_version_checkBox); // 确保ID和变量名正确
}


BEGIN_MESSAGE_MAP(fct_cfg_options, CDialogEx)
	ON_BN_CLICKED(IDC_CFG_SAVE, &fct_cfg_options::OnBnClickedCfgSave)
	ON_BN_CLICKED(IDC_CFG_WRETE_IMEI, &fct_cfg_options::OnBnClickedCfgWreteImei)
	ON_BN_CLICKED(IDC_CFG_READ_VERSION, &fct_cfg_options::OnBnClickedCfgReadVersion)
	ON_BN_CLICKED(IDC_CFG_READ_IMEI, &fct_cfg_options::OnBnClickedCfgReadImei)
	ON_BN_CLICKED(IDC_CFG_READ_CARD, &fct_cfg_options::OnBnClickedCfgReadCard)
	ON_BN_CLICKED(IDC_CFG_LTE_NB_CAL, &fct_cfg_options::OnBnClickedCfgLteNbCal)
	ON_BN_CLICKED(IDC_CFG_WCDMA_CAL, &fct_cfg_options::OnBnClickedCfgWcdmaCal)
	ON_BN_CLICKED(IDC_CFG_GSM_CAL, &fct_cfg_options::OnBnClickedCfgGsmCal)
	ON_BN_CLICKED(IDC_CFG_READ_CSQ, &fct_cfg_options::OnBnClickedCfgReadCsq)
	ON_BN_CLICKED(IDC_CFG_READ_NET, &fct_cfg_options::OnBnClickedCfgReadNet)
	ON_BN_CLICKED(IDC_CFG_READ_MODE, &fct_cfg_options::OnBnClickedCfgReadMode)
END_MESSAGE_MAP()

// 在对话框实现文件中初始化
BOOL fct_cfg_options::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 初始化复选框状态
	m_write_imei_checkBox.SetCheck(BST_UNCHECKED);
	m_read_imei_checkBox.SetCheck(BST_UNCHECKED);
	m_read_card_checkBox.SetCheck(BST_UNCHECKED);
	m_lte_nb_cal_checkBox.SetCheck(BST_UNCHECKED);
	m_wcdma_cal_checkBox.SetCheck(BST_UNCHECKED);
	m_gsm_cal_checkBox.SetCheck(BST_UNCHECKED);
	m_read_csq_checkBox.SetCheck(BST_UNCHECKED);
	m_read_network_checkBox.SetCheck(BST_UNCHECKED);
	m_read_mode_checkBox.SetCheck(BST_UNCHECKED);
	m_read_version_checkBox.SetCheck(BST_UNCHECKED);

	if (getCfgJsonData() != NULL) {
		m_write_imei_checkBox.SetCheck(test_cfg.write_imei);
		m_read_imei_checkBox.SetCheck(test_cfg.read_imei);
		m_read_card_checkBox.SetCheck(test_cfg.read_card);
		m_lte_nb_cal_checkBox.SetCheck(test_cfg.lte_nb_cal);
		m_wcdma_cal_checkBox.SetCheck(test_cfg.wcdma_cal);
		m_gsm_cal_checkBox.SetCheck(test_cfg.gsm_cal);
		m_read_csq_checkBox.SetCheck(test_cfg.read_csq);
		m_read_network_checkBox.SetCheck(test_cfg.read_network);
		m_read_mode_checkBox.SetCheck(test_cfg.read_mode);
		m_read_version_checkBox.SetCheck(test_cfg.read_version);
		GetDlgItem(IDC_CFG_VERSION_EDIT)->SetWindowText(CString(test_cfg.read_version_edit.c_str()));
	}
	return TRUE;  // 返回 TRUE，除非将焦点设置到控件
}


#include <atlconv.h> // 包含 ATL 转换宏
std::string CStringToStdString(const CString& cstr)
{
#ifdef _UNICODE
	// Unicode 项目
	CT2A pszConverted(cstr);
	return std::string(pszConverted);
#else
	// 多字节项目
	return std::string(cstr.GetString());
#endif
}

test_cfg_options* fct_cfg_options::getCfgJsonData() {
	// 从文件中读取 JSON 数据
	std::ifstream i("cfg\\test_cfg.json");
	if (!i.is_open()) {
		AfxMessageBox(_T("Failed to open file!"));
		return NULL;
	}

	json j_read;
	try {
		i >> j_read; // 解析 JSON 数据
	}
	catch (const json::parse_error& e) {
		i.close();
		return NULL;
	}
	i.close();

	test_cfg.write_imei = j_read["WriteImei"];
	test_cfg.read_imei = j_read["ReadImei"];
	test_cfg.read_card = j_read["ReadCard"];
	test_cfg.lte_nb_cal = j_read["LteNbCal"];
	test_cfg.wcdma_cal = j_read["WcdmaCal"];
	test_cfg.gsm_cal = j_read["GsmCal"];
	test_cfg.read_csq = j_read["ReadCsq"];
	test_cfg.read_network = j_read["ReadNet"];
	test_cfg.read_mode = j_read["ReadMode"];
	test_cfg.read_version = j_read["ReadVersion"];
	test_cfg.read_version_edit = j_read["ReadVersionStr"];

	return &test_cfg;
}

void fct_cfg_options::OnBnClickedCfgSave()
{
	// TODO: 在此添加控件通知处理程序代码
	CString version_edit;
	GetDlgItem(IDC_CFG_VERSION_EDIT)->GetWindowText(version_edit);
	test_cfg.read_version_edit = CStringToStdString(version_edit);

	json j;
	j["WriteImei"] = test_cfg.write_imei;
	j["ReadImei"] = test_cfg.read_imei;
	j["ReadCard"] = test_cfg.read_card;
	j["LteNbCal"] = test_cfg.lte_nb_cal;
	j["WcdmaCal"] = test_cfg.wcdma_cal;
	j["GsmCal"] = test_cfg.gsm_cal;
	j["ReadCsq"] = test_cfg.read_csq;
	j["ReadNet"] = test_cfg.read_network;
	j["ReadMode"] = test_cfg.read_mode;
	j["ReadVersion"] = test_cfg.read_version;
	j["ReadVersionStr"] = test_cfg.read_version_edit;

	// 文件夹名称
	CString folderName("cfg");

	// 检查并创建文件夹
	if (!CreateDirectory(folderName, NULL)) {
		DWORD error = GetLastError();
		if (error != ERROR_ALREADY_EXISTS) {
			return ;
		}
	}

	// 文件路径
	std::string filePath = folderName + "\\test_cfg.json";

	// 打开文件并写入数据
	std::ofstream outFile(filePath);
	if (!outFile.is_open()) {
		return ;
	}

	// 将 JSON 对象写入文件
	std::ofstream o(filePath);
	o << std::setw(4) << j << std::endl;
	o.close();

	AfxMessageBox(_T("已保存！"));
}

// fct_cfg_options 消息处理程序
void fct_cfg_options::OnBnClickedCfgWreteImei()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_write_imei_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.write_imei = 1;
	}
	else {
		test_cfg.write_imei = 0;
	}

}

void fct_cfg_options::OnBnClickedCfgReadImei()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_imei_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_imei = 1;
	}
	else {
		test_cfg.read_imei = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgReadCard()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_card_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_card = 1;
	}
	else {
		test_cfg.read_card = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgLteNbCal()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_lte_nb_cal_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.lte_nb_cal = 1;
	}
	else {
		test_cfg.lte_nb_cal = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgWcdmaCal()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_wcdma_cal_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.wcdma_cal = 1;
	}
	else {
		test_cfg.wcdma_cal = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgGsmCal()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_gsm_cal_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.gsm_cal = 1;
	}
	else {
		test_cfg.gsm_cal = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgReadCsq()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_csq_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_csq = 1;
	}
	else {
		test_cfg.read_csq = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgReadNet()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_network_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_network = 1;
	}
	else {
		test_cfg.read_network = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgReadMode()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_mode_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_mode = 1;
	}
	else {
		test_cfg.read_mode = 0;
	}
}

void fct_cfg_options::OnBnClickedCfgReadVersion()
{
	// TODO: 在此添加控件通知处理程序代码
	BOOL isChecked = m_read_version_checkBox.GetCheck();
	if (isChecked == BST_CHECKED) {
		test_cfg.read_version = 1;
	}
	else {
		test_cfg.read_version = 0;
	}

}
